//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ExchangeContract.h"
using namespace Cephei::QL::Experimental::Commodities;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract (String^ code, DateTime expirationDate, DateTime underlyingStartDate, DateTime underlyingEndDate) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phExchangeContract = NULL;
#endif
        std::string _code = (std::string)ValueHelper::Convert (code); //d
        QuantLib::Date _expirationDate = (QuantLib::Date)ValueHelper::Convert (expirationDate); //d
        QuantLib::Date _underlyingStartDate = (QuantLib::Date)ValueHelper::Convert (underlyingStartDate); //d
        QuantLib::Date _underlyingEndDate = (QuantLib::Date)ValueHelper::Convert (underlyingEndDate); //d
        _ppExchangeContract = new boost::shared_ptr<QuantLib::ExchangeContract> (new QuantLib::ExchangeContract ( _code,  _expirationDate,  _underlyingStartDate,  _underlyingEndDate ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phExchangeContract = NULL;
#endif
        _ppExchangeContract = new boost::shared_ptr<QuantLib::ExchangeContract> (new QuantLib::ExchangeContract ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract (boost::shared_ptr<QuantLib::ExchangeContract>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeContract = NULL;
#endif
	_ppExchangeContract = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract (QuantLib::ExchangeContract& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phExchangeContract = NULL;
#endif
	_ppExchangeContract = new boost::shared_ptr<QuantLib::ExchangeContract> (&childNative);
    
    _ExchangeContractOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract (CExchangeContract^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeContract = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExchangeContract = new boost::shared_ptr<QuantLib::ExchangeContract> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeContract = NULL;
#endif
	if (!t->IsSubclassOf(CExchangeContract::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract (QuantLib::Handle<QuantLib::ExchangeContract>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExchangeContract = &childNative;
	_ppExchangeContract = &static_cast<boost::shared_ptr<QuantLib::ExchangeContract>>(childNative.currentLink());
    
    _ExchangeContractOwner = owner;
}
Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract (QuantLib::Handle<QuantLib::ExchangeContract> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phExchangeContract = &childNative;
	_ppExchangeContract = &static_cast<boost::shared_ptr<QuantLib::ExchangeContract>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CExchangeContract::CExchangeContract (QuantLib::ExchangeContract childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phExchangeContract = NULL;
#endif
	_ppExchangeContract = new boost::shared_ptr<QuantLib::ExchangeContract> (new QuantLib::ExchangeContract (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CExchangeContract::~CExchangeContract ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppExchangeContract != NULL)
    {
	    delete _ppExchangeContract;
        _ppExchangeContract = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CExchangeContract::!CExchangeContract ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppExchangeContract != NULL)
    {
	    delete _ppExchangeContract;
    }
}
QuantLib::ExchangeContract& Cephei::QL::Experimental::Commodities::CExchangeContract::GetReference ()
{
    if (_ppExchangeContract == NULL) throw REFNEW NativeNullException ();
	return **_ppExchangeContract;
}
boost::shared_ptr<QuantLib::ExchangeContract>& Cephei::QL::Experimental::Commodities::CExchangeContract::GetShared ()
{
    if (_ppExchangeContract == NULL) throw REFNEW NativeNullException ();
	return *_ppExchangeContract;
}
QuantLib::ExchangeContract* Cephei::QL::Experimental::Commodities::CExchangeContract::GetPointer ()
{
    if (_ppExchangeContract == NULL) throw REFNEW NativeNullException ();
	return &**_ppExchangeContract;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ExchangeContract>& Cephei::QL::Experimental::Commodities::CExchangeContract::GetHandle ()
{
	if (_phExchangeContract == NULL)
	{
		_phExchangeContract = new Handle<QuantLib::ExchangeContract> (*_ppExchangeContract);
	}
	return *_phExchangeContract;
}
#endif
bool Cephei::QL::Experimental::Commodities::CExchangeContract::HasNative () 
{
	return (_ppExchangeContract != NULL);
}

String^ Cephei::QL::Experimental::Commodities::CExchangeContract::Code::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppExchangeContract)->code ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Commodities::CExchangeContract::ExpirationDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppExchangeContract)->expirationDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Commodities::CExchangeContract::UnderlyingEndDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppExchangeContract)->underlyingEndDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Commodities::CExchangeContract::UnderlyingStartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppExchangeContract)->underlyingStartDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::IExchangeContract^ Cephei::QL::Experimental::Commodities::CExchangeContract_Factory::Create (String^ code, DateTime expirationDate, DateTime underlyingStartDate, DateTime underlyingEndDate)
{
    return REFNEW CExchangeContract ( code,  expirationDate,  underlyingStartDate,  underlyingEndDate);
}
Cephei::QL::Experimental::Commodities::IExchangeContract^ Cephei::QL::Experimental::Commodities::CExchangeContract_Factory::Create ()
{
    return REFNEW CExchangeContract ();
}
